package com.py.pythink.service.impl;

import com.py.pythink.config.SystemConfig;
import com.py.pythink.dao.UserDetailDao;
import com.py.pythink.dao.UserFollowDao;
import com.py.pythink.dao.UserSendDao;
import com.py.pythink.entity.UserDetail;
import com.py.pythink.entity.UserFollow;
import com.py.pythink.entity.UserSend;
import com.py.pythink.service.intf.UserFollowService;
import com.py.pythink.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.ServerError;
import java.util.List;

@Service
public class UserFollowServiceImpl implements UserFollowService {
    @Autowired
    private UserFollowDao followDao;
    @Autowired
    private UserSendDao sendDao; //帖子的Dao
    @Autowired
    private UserDetailDao userDetailDao;
    @Override
    public R attention(int uid, int fuid) {
        System.err.println(uid);
        System.err.println(fuid);
        UserSend send = sendDao.selectById(fuid);//根据前端传来的关注的帖子id 也就是fuid，得到帖子对象
        List<UserSend> sendnumber = sendDao.sendnumber(send.getUid());//根据发帖人的id,得到他发过所有帖子

        List<UserFollow> listattention = followDao.listattention(uid);
        System.out.println(listattention);
        if(listattention.size()==0){

            System.err.println(uid+"sssssssssssssssss"+fuid);
            UserSend userSend = sendDao.selectById(fuid);
            System.err.println(userSend);
            userSend.getNickname();//发帖人的名称
            UserDetail userDetail = userDetailDao.selectByUid(uid);
            System.err.println(userDetail);
            if(userDetail.getNickname()==null){
                return R.ok("亲，先完善个人信息 才能关注");
            };//关注人的名称
            UserFollow userFollow1 = new UserFollow(uid, userDetail.getNickname(), fuid, userSend.getNickname());
            followDao.insertt(userFollow1);
            return R.ok("亲，成功关注该用户");
        }
        for (UserSend list : sendnumber) {//为了得到发帖子发过帖子的id
            for (UserFollow ll : listattention) {
                if (list.getId() == ll.getFuid()) {
                    UserFollow userFollow = followDao.getInstance(uid, list.getId());
                  if(userFollow.getFlag() == SystemConfig.USER_ATTENTION_NO){
                      followDao.updateflag(SystemConfig.USER_ATTENTION_YES, uid, list.getId());
                      return R.ok("已经关注了宝");
                  }else{
                      followDao.updateflag(SystemConfig.USER_ATTENTION_NO, uid, list.getId());
                      return R.ok("亲，已经取消关注了");
                  }


//                    if (userFollow == null) {
//                        UserSend userSend = sendDao.selectById(fuid);
//                        userSend.getNickname();//发帖人的名称
//                        UserDetail userDetail = userDetailDao.selectByUid(uid);
//                        userDetail.getNickname();//关注人的名称
//                        UserFollow userFollow1 = new UserFollow(uid, userDetail.getNickname(), fuid, userSend.getNickname());
//                        followDao.insertt(userFollow1);
//                        return R.ok("亲，成功关注该用户");
//                    } else if (userFollow.getFlag() == SystemConfig.USER_ATTENTION_NO) {
//                        followDao.updateflag(SystemConfig.USER_ATTENTION_YES, uid, fuid);
//                        return R.ok("已经关注了宝");
//                    } else if (userFollow.getFlag() == SystemConfig.USER_ATTENTION_YES) {
//                        followDao.updateflag(SystemConfig.USER_ATTENTION_NO, uid, fuid);
//                        return R.ok("亲，已经取消关注了");
//                    } else {
//                        return R.ok("确定要取消关注嘛，你们是互相关注的！！！");
//                    }

                }
            }
        }
        return R.fail("网络异常");

//        UserFollow userFollow = followDao.getInstance(uid, fuid);
//
//        if(userFollow==null){
//            UserSend userSend = sendDao.selectById(fuid);
//            userSend.getNickname();//发帖人的名称
//            UserDetail userDetail = userDetailDao.selectByUid(uid);
//            userDetail.getNickname();//关注人的名称
//            UserFollow userFollow1 = new UserFollow(uid,userDetail.getNickname(),fuid,userSend.getNickname());
//            followDao.insertt(userFollow1);
//            return R.ok("亲，成功关注该用户");
//            }else if(userFollow.getFlag()== SystemConfig.USER_ATTENTION_NO){
//                followDao.updateflag(SystemConfig.USER_ATTENTION_YES,uid,fuid);
//                return R.ok();
//            }else if(userFollow.getFlag()== SystemConfig.USER_ATTENTION_YES){
//                followDao.updateflag(SystemConfig.USER_ATTENTION_NO,uid,fuid);
//                return R.ok();
//            }else{
//                return R.ok("确定要取消关注嘛，你们是互相关注的！！！");
//            }



    }

}
